// ignore_for_file: must_be_immutable, unrelated_type_equality_checks
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/model/info_item.dart';
import 'package:yt_devops/pages/alarm/alarm_home/alarm_list/alarm_list_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';

class AlarmListItemView extends StatelessWidget {
  AlarmListItemView({
    required this.model,
    required this.onItemTaped,
    required this.isHistory,
    super.key,
  });

  AlarmModel model = AlarmModel();

  ///是否是历史告警
  final bool isHistory;
  final void Function(AlarmModel model) onItemTaped;

  @override
  Widget build(BuildContext context) {
    // Log.d('model===' + model.name.toString());
    return GestureDetector(
      onTap: () {
        Log.d('AlarmListItemView==='+ model.toString());
        FocusScope.of(context).requestFocus(FocusNode());
        onItemTaped(model);
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 10.h,right: 16.w,left: 16.w),
        padding: EdgeInsets.all(16.w),
        decoration: BoxDecoration(
          color: Colour.white,
          borderRadius: BorderRadius.circular(8),
        ),
        child: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(bottom: 14.h),
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Row(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(right: 4.w),
                          child: Image.asset(
                            FuncUtils().judgeAlarmIcon(model.level!),
                            width: 24.w,
                            height: 24.h,
                          ),
                        ),
                        Expanded(
                          child: SizedBox(
                            // width: 260.w,
                            height: 24.h,
                            child: Text(
                              model.name ?? '',
                              style: TextStyle(
                                color: Colour.mainTextColor,
                                fontWeight: FontWeight.w500,
                                fontSize: 18.sp,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),

                  _statusWidget(model.level),
                ],
              ),
            ),
            Visibility(
              // visible: isHistory,
              child: _itemWidget(
              '${ResKey.siteName.tr}: ', model.siteName?.toString() ?? ''),
            ),
            Visibility(
                visible: !isHistory,
                child: _itemWidget(
                    '${ResKey.alarmStatus.tr}: ', model.status!.label)),
            Visibility(
                visible: !isHistory,
                child: _itemWidget('${ResKey.deviceName.tr}: ',
                    model.deviceName?.toString() ?? '')),
            Visibility(
                visible: !isHistory,
                child: _itemWidget('${ResKey.generationTime.tr}: ',
                    model.alarmTime?.toString() ?? '')),
            // Visibility(
            //     visible: isHistory,
            //     child: _itemWidget('${ResKey.generationTime.tr}: ',
            //         model.alarmTime?.toString() ?? '')),
            Visibility(
                visible: isHistory,
                child: _itemWidget('${ResKey.deviceName.tr}: ',
                model.deviceName?.toString() ?? '')),
            Visibility(
                visible: isHistory,
                child: _itemWidget('${ResKey.generationTime.tr}: ',
                    model.alarmTime?.toString() ?? '')),
            Visibility(
                visible: isHistory,
                child:_itemWidget('${ResKey.eliminatingTypes.tr}: ',
                    model.recoveryByName?.toString() ?? ResKey.automaticRecovery.tr)),
            Visibility(
                visible: isHistory,
                child:_itemWidget('${ResKey.eliminationTime.tr}: ',
                    model.recoveryTime?.toString() ?? '')),
          ],
        ),
      ),
    );
  }

  _itemWidget(String label, String value) {
    return Container(
      margin: EdgeInsets.only(top: 4.h),
      child: Row(
        children: [
          SizedBox(
              width: 98.w,
              child: Text(label,
                  maxLines: 2,
                  style: TextStyles.lf14)),
          Expanded(
              child: Text(
            value,
            style: value == AlarmStatusType.generate.label
                ? TextStyles.f14.copyWith(color: Colour.errorTextColor)
                : TextStyles.f14,
            overflow: TextOverflow.ellipsis,
          )),
        ],
      ),
    );
  }

  Widget _statusWidget(AlarmStatusGradeType? alarmType) {
    Color color;

    switch (alarmType) {
      case AlarmStatusGradeType.error:
        color = const Color(0xFFFA3F3F);
        break;

      case AlarmStatusGradeType.alarm:
        color = const Color(0xFFF79631);
        break;

      case AlarmStatusGradeType.warn:
        color = const Color(0xFFFAC958);
        break;

      default:
        color = const Color(0xFF0F6EFF);
        break;
    }

    return Container(
      // width: 32.w,
      alignment: Alignment.center,
      height: 22,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(4.sp)),
        color: color,
      ),
      child:
      Padding(
        padding: const EdgeInsets.only(top: 4,left: 4,right: 4),
        child:
        Text(
          alarmType!.label,
          textAlign: TextAlign.center,
          style: TextStyles.styleWith(
            12.sp,
            color: Colour.white,
          ),
        ),
      ),
    );
  }
}
